package com.it;

import java.util.Arrays;
import java.util.Scanner;

public class CalTotalAVg {
    public static void main(String[] args) {
        /**
         * 有n位评委给选手打分，选手的最终得分为：去掉最高分和最低分的n-2为评委的平均分
         * 要求用方法实现
         */
        double[] scores = inputData(6);//录取评委的评分
        double avg = calAvg(scores);//平均分数
        System.out.println("avg = " + avg);
        //double a = (100 + 80 + 97 + 98 + 99 + 89 - 100 - 80) / 4;
        //System.out.println("a = " + a);//95.0
    }


    /**
     * int n 参数是n个评委
     * 该方法录取n个评委的评分
     */
    public static double[] inputData(int n) {
        double[] scores = new double[n];//评分数组
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < scores.length; i++) {
            System.out.println("请输入" + (i + 1) + "位评委的评分");
            scores[i] = scanner.nextDouble();//让每个评委打分，并把分数赋值给元素
        }
        return scores;
    }

    /**
     * 该方法计算平均分
     */
    public static double calAvg(double[] scores) {
        Arrays.sort(scores);//升序排序，即从小到大排序

        double sum = 0.0;//假设总分
        double max = scores[0];//假设最高分
        double min = scores[0];//假设最低分

        for (int i = 0; i < scores.length; i++) {
            sum += scores[i];
            if (max < scores[i]) {
                max = scores[i];
            }
            if (min > scores[i]) {
                min = scores[i];
            }
        }

        double avg = (sum - max - min) / (scores.length - 2);
        return avg;
    }
}
